package cn.controller;

import cn.entity.Result;
import cn.entity.SysOffice;
import cn.service.SysOfficeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/office")
public class SysOfficeController {
    @Autowired
    SysOfficeService sysOfficeService;


    @RequestMapping("")
    public ModelAndView office(){
        return new ModelAndView("office/office");
    }

    @RequestMapping("detail")
    public ModelAndView detail(){
        return new ModelAndView("office/detail");
    }

    @RequestMapping("update")
    public ModelAndView update(){
        return new ModelAndView("office/office-detail");
    }

    @RequestMapping("/select")
    public Result list(){
        SysOffice sysOffice = new SysOffice();
        sysOffice.setDelFlag("0");
        return new Result(true,"成功",sysOfficeService.select(sysOffice));
    }
    @RequestMapping("/select/{pageNum}/{pageSize}")
    public Result select(@PathVariable("pageNum")int pageNum, @PathVariable("pageSize")int pageSize){
        return new Result(true,"成功",sysOfficeService.selectPage(pageNum,pageSize));
    }

    @RequestMapping("/selectByName/{pageNum}/{pageSize}")
    public Result selectByName(@PathVariable("pageNum")int pageNum, @PathVariable("pageSize")int pageSize,String officeName){
        return new Result(true,"成功",sysOfficeService.selectPageByOfficeName(pageNum,pageSize,officeName));
    }

    @RequestMapping("/updateOffice")
    public Result updateOffice(@RequestBody SysOffice sysOffice){
        sysOffice.setUpdateDate(new Date());
        return new Result(true,"更新成功",sysOfficeService.updateByPrimaryKeySelective(sysOffice));
    }

    @RequestMapping("/deleteOffice")
    public Result deleteOffice(@RequestBody SysOffice sysOffice){
        sysOffice.setDelFlag("1");
        sysOffice.setUpdateDate(new Date());
        return new Result(true,"删除成功",sysOfficeService.updateByPrimaryKeySelective(sysOffice));
    }

}
